<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{margin: 0; padding: 0; font-family: "Arial", "微软雅黑";}
        ul,li{list-style: none;}
        .bbs{margin: 0 auto; width: 600px; position: relative;}
        header{padding: 5px 0; border-bottom: 1px solid #cecece;}
        header span{display:inline-block; width: 220px; height: 50px; color: #fff; background: #009966; font-size: 18px; font-weight: bold; text-align: center;line-height: 50px; border-radius: 8px; cursor: pointer;}
        .post{position: absolute; background: #ffffff; border: 1px #cccccc solid; width: 500px; left: 65px; top:70px; padding: 10px; font-size: 14px; z-index: 999999; display: none;}
        .post .title{width: 450px; height:30px; line-height: 30px; display: block; border: 1px #cecece solid; margin-bottom: 10px;}
        .post select{width: 200px; height: 30px;}
        .post .content{width: 450px; height: 200px; display: block; margin: 10px 0;border: 1px #cecece solid;}
        .post .btn{width: 160px; height: 35px; color: #fff; background: #009966; border: none; font-size: 14px; font-weight: bold; text-align: center; line-height: 35px; border-radius: 8px; cursor: pointer;}

        .bbs section ul li{padding: 10px 0; border-bottom: 1px #999999 dashed;
            overflow: hidden;}
        .bbs section ul li div{float: left; width: 60px; margin-right: 10px;}
        .bbs section ul li div img{ border-radius:50%; width: 60px;}
        .bbs section ul li h1{float: left; width: 520px; font-size: 16px; line-height: 35px;}
        .bbs section ul li p{color: #666666; line-height: 25px; font-size: 12px; }
        .bbs section ul li p span{padding-right:20px;}

    </style>
</head>
<body>
<div class="bbs">
    <header><span>我要发帖</span></header>
    <section>
        <ul id="postList"></ul>
    </section>
    <div class="post" id="post">
        <input class="title" placeholder="请输入标题（1-50个字符）" id="title">
        所属版块：<select id="sec"><option>请选择版块</option><option>电子书籍</option><option>新课来了</option><option>新手报到</option><option>职业规划</option></select>
        <textarea class="content" id="content"></textarea>
        <input class="btn" value="发布">
    </div>
</div>
    <script>
        let distribute = document.getElementsByClassName("bbs")[0].firstElementChild.firstElementChild;
        let model = document.getElementById("post");
        let headPrt = ["img/tou01.jpg","img/tou02.jpg","img/tou03.jpg","img/tou04.jpg"];
        distribute.onclick = function () {
            model.style.display = "block";
        }
        let isFirst = true;
        let issue = model.lastElementChild;
        issue.addEventListener("click",function () {
            let head = model.firstElementChild;
            let headValue = head.value;
            let content = model.lastElementChild.previousElementSibling;
            let contentValue = content.innerHTML;
            let select = model.firstElementChild.nextElementSibling;
            let plateArr = select.children;
            let plateArrValue;
            for (let i = 0; i < plateArr.length; i++) {
                if (plateArr[i].selected){
                    plateArrValue = plateArr[i].innerHTML;
                    console.log(plateArrValue);
                    break;
                }
            }
            let newLi = document.createElement('li');
            let newDiv = document.createElement('div');
            let newImg = document.createElement('img');
            let newH1 = document.createElement('h1');
            let newP = document.createElement('p');
            let newSpan1 = document.createElement('span');
            let newSpan2 = document.createElement('span');
            newImg.src = headPrt[Math.floor(Math.random() * 4)];
            newImg.alt = "美食";
            newDiv.appendChild(newImg);
            newH1.innerHTML = headValue;
            newSpan1.innerHTML = "板块:" + plateArrValue;
            let time = new Date();
            let year = time.getFullYear();
            let month = time.getMonth() + 1;
            let day = time.getDate();
            let hour = time.getHours();
            let minutes = time.getMinutes();
            newSpan2.innerHTML = "发表时间：" + year + "-" + month + "-" + day + " " + hour + ":" + minutes;
            newP.appendChild(newSpan1);
            newP.appendChild(newSpan2);
            newLi.appendChild(newDiv);
            newLi.appendChild(newH1);
            newLi.appendChild(newP);
            let ul = document.getElementById("postList");
            let oldLi = ul.firstElementChild;
            if (isFirst){
                ul.appendChild(newLi);
                isFirst = false;
            } else{
                oldLi.before(newLi);//before是用于本元素的方法
            }
            model.style.display = "none";
            let text = document.getElementById("content");
            text.value = "";
            model.firstElementChild.value = "";
            let def = document.getElementById("sec");
            def.firstElementChild.selected = true;
            /*console.log(headValue);*/
        },false);

    </script>
</body>
</html>